package cn.csg.iotplatform.module.system.controller.admin.user.vo.user;

import cn.csg.iotplatform.framework.mybatis.core.type.JsonLongSetTypeHandler;
import cn.csg.iotplatform.module.system.util.ToolUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class PageCopyUserRoleDo {

    private Long userId;

    private String userName;

    private String nickName;

    private Long deptId;

    private String deptName;

    //@TableField(typeHandler = JsonLongSetTypeHandler.class)
    private Set<Long> relationDeptIds;

    private String relationDeptIdsStr;


    public Set<Long> getRelationDeptIds() {
        return convertStringToSet(relationDeptIdsStr);
    }

    public void setRelationDeptIds(Set<Long> relationDeptIds) {
        this.relationDeptIds = convertStringToSet(relationDeptIdsStr);
    }

    public static Set<Long> convertStringToSet(String str) {
        if (ToolUtil.isEmpty(str)) {
            return new HashSet<>();
        }

        // 移除字符串两端的方括号
        str = str.substring(1, str.length() - 1);
        if (ToolUtil.isEmpty(str)) {
            return new HashSet<>();
        }

        // 使用逗号分割字符串，并转换为Long类型的流
        Stream<Long> longStream = Stream.of(str.split(","))
                .map(String::trim) // 移除每个元素两端的空白字符
                .map(Long::parseLong); // 将字符串转换为Long

        // 收集流中的元素到Set中
        return longStream.collect(Collectors.toSet());
    }
}
